#pragma once

#include "Reactor.hpp"
#include "Sock.hpp"
#include "Server.hpp"
#include "Util.hpp"

int Accepter(Event *evp)
{
    // 由于我们使用的是ET模式，只会提醒一次，所以要一次读取完
    while (1)
    {
        int listen_sock = evp->sock;
        int new_sock = Sock::Accept(listen_sock);

        if (new_sock < 0)
        {
            std::cout << "accepted done" << std::endl;
            break;
        }
        std::cout << "有新连接就绪：";
        std::cout << new_sock << std::endl;

        // 设置新文件描述符为非阻塞
        SetNonBlock(new_sock);

        Event *new_evp = new Event;
        new_evp->sock = new_sock;
        new_evp->R = evp->R;
        new_evp->RegisterCallback(Recver, Sender, Errorer);

        // 设置为非阻塞加入反应堆
        new_evp->R->InsertEvent(new_evp, EPOLLIN | EPOLLET);
    }
    return 0;
}